/* Pically
 * Copyright (C) 2011 Petros Dolaschjan.  All rights reserved.
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 *  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 *  ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
 *  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 *  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package de.pd.pically.gui;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;

import net.iharder.dnd.FileDrop;

import org.jdesktop.application.Action;
import org.jdesktop.application.Application;
import org.jdesktop.application.ApplicationActionMap;

import de.pd.pically.gui.JFileChooserPreview.PreviewPane;
import de.pd.pically.util.ImageUtil;
import de.pd.pically.util.UserSession;
import de.pd.pically.util.Utils;

/**
 * @author petros
 *
 */
@SuppressWarnings("serial")
public class MonthPicPanel extends JPanel {

	private String mImgPath;
	
	private JPanel mContentPanel;
	private ApplicationActionMap mActionMap;

	public MonthPicPanel(int pMonth) {
		setLayout(new GridLayout());
		mActionMap = Application.getInstance().getContext()
				.getActionMap(this);
		setBorder(new LineBorder(new Color(204, 204, 204)));
		
		
		mContentPanel = new JPanel();
		mContentPanel.setMinimumSize(new Dimension(145, 115));
		mContentPanel.setMaximumSize(new Dimension(145, 115));
		PTitlePanel panelMain = new PTitlePanel(Utils.getMonthName(pMonth), mContentPanel);
		panelMain.setBgColor(new Color(208, 225, 255));
		//panelMain.setContentContainer(mContentPanel);
		
		// content
		mContentPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
		JButton browseButton = new JButton(mActionMap.get("browseAction"));
		browseButton.setText("Select Picture");
		mContentPanel.add(browseButton);
		add(panelMain);
		
		new FileDrop( System.out, mContentPanel, /*dragBorder,*/ new FileDrop.Listener()
        {   public void filesDropped( java.io.File[] files )
            {   for( int i = 0; i < files.length; i++ )
                {   try
                    {   setImage( files[0].getCanonicalPath() );
                    }   // end try
                    catch( java.io.IOException e ) {}
                }   // end for: through each dropped file
            }   // end filesDropped
        }); // end FileDrop.Listener

	}
	
	
	@Action
	public void browseAction() {
		JFileChooser jfc = new JFileChooser();
		PreviewPane previewPane = new PreviewPane();
		jfc.addChoosableFileFilter(new ImageFilter());
		jfc.setAcceptAllFileFilterUsed(false);
		jfc.setAccessory(previewPane);
		jfc.addPropertyChangeListener(previewPane);
		jfc.setMultiSelectionEnabled(false);
		jfc.setFileSelectionMode(JFileChooser.FILES_ONLY);
		jfc.setCurrentDirectory(new File(UserSession.lastSelectedDir));
		int selection = jfc.showOpenDialog(getParent());
		String imgPath = null;
		if (selection == JFileChooser.APPROVE_OPTION) {
			imgPath = jfc.getSelectedFile().getAbsolutePath();
			UserSession.lastSelectedDir = imgPath;
		} else {
			return;
		}
		
		setImage(imgPath);
	}


	/**
	 * @param imgPath
	 */
	private void setImage(String imgPath) {
		if(imgPath == null) return;
		// check if the image is valid
		int height = mContentPanel.getMaximumSize().width -25;
		ImageIcon icon = ImageUtil.getThumbnail(imgPath, height);
		if(icon == null) {
			JOptionPane.showMessageDialog(getParent(), "The picture you have selected is not valid", "Error",
					JOptionPane.ERROR_MESSAGE);
			return;
		}
		
		mImgPath = imgPath;
		mContentPanel.removeAll();
		JLabel lblIcon = new JLabel(icon);
		
		lblIcon.setCursor(new Cursor(Cursor.HAND_CURSOR));
		lblIcon.setToolTipText("Click to choose another image");
		lblIcon.addMouseListener(new MouseAdapter() {

			@Override
			public void mouseClicked(MouseEvent pArg0) {
				browseAction();
			}
			
		});
		mContentPanel.add(lblIcon);
		updateUI();
	}

	public String getImgPath() {
		return mImgPath;
	}

	public void setImgPath(String pImgPath) {
		mImgPath = pImgPath;
	}
	
}
